<template>
  <div class="layout-container">
    <el-container>
      <el-aside width="220px">
        <div class="logo">
          <h2>体测管理系统</h2>
        </div>
        <el-menu router :default-active="activeMenu" class="menu" background-color="#304156" text-color="#bfcbd9" active-text-color="#409EFF">
          <el-menu-item index="/admin/dashboard">
            <el-icon>
              <DataLine />
            </el-icon>
            <span>控制台</span>
          </el-menu-item>
          <el-menu-item index="/admin/students">
            <el-icon>
              <User />
            </el-icon>
            <span>学生管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/sport-items">
            <el-icon>
              <List />
            </el-icon>
            <span>体测项目</span>
          </el-menu-item>
          <el-menu-item index="/admin/scores">
            <el-icon>
              <Document />
            </el-icon>
            <span>成绩管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/statistics">
            <el-icon>
              <PieChart />
            </el-icon>
            <span>统计分析</span>
          </el-menu-item>
          <el-menu-item index="/admin/announcements">
            <el-icon>
              <Message />
            </el-icon>
            <span>公告管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/profile">
            <el-icon>
              <Setting />
            </el-icon>
            <span>个人中心</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <div class="header-right">
            <el-dropdown trigger="click" @command="handleCommand">
              <span class="user-info">
                <img :src="userAvatar" class="user-avatar" />
                {{ userName }}
                <el-icon>
                  <ArrowDown />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { DataLine, User, List, Document, PieChart, Message, Setting, ArrowDown } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()

// 默认头像
const defaultAvatar = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'

// 用户信息
const userInfo = ref({})
const userName = computed(() => userInfo.value.name || '管理员')
const userAvatar = computed(() => userInfo.value.avatarUrl || defaultAvatar)

// 当前激活的菜单项
const activeMenu = computed(() => route.path)

// 初始化用户信息
onMounted(() => {
  const storedUserInfo = localStorage.getItem('userInfo')
  if (storedUserInfo) {
    userInfo.value = JSON.parse(storedUserInfo)
  }
})

// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'profile') {
    router.push('/admin/profile')
  } else if (command === 'logout') {
    ElMessageBox.confirm('确认退出登录吗?', '提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      // 清除本地存储的用户信息和token
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      // 跳转到登录页
      router.push('/')
    }).catch(() => { })
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100vw;
}

.logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  background-color: #263445;
}

.logo h2 {
  margin: 0;
  color: #fff;
  font-size: 18px;
}

.menu {
  height: calc(100% - 60px);
  border-right: none;
}

.el-aside {
  background-color: #304156;
  color: #fff;
}

.el-header {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 60px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 8px;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style>